<?php 
	 if (!isset($_SESSION['login']) || !isset($_SESSION['senha'])) {
 	header('location: login.html');
 }
    if($_SESSION['permissao'] === '1' || $_SESSION['permissao'] === '3'){
 	   	 header('location: index.php');
 	   }
	if (isset($_GET['alert']) && !empty($_GET['alert'])) {
		$alert =$_GET['alert'];
		echo "<script type='text/javascript'>alert('$alert');</script>";
	}

 	$sql = "SELECT * FROM funcao";
 	$resultado = mysql_query($sql) or die(mysql_error());

 	$sqlperm = "SELECT * FROM permissoes";
 	$resulperm = mysql_query($sqlperm) or die(mysql_error());
 
 ?>
 <html>
 <head>
 	<script type="text/javascript">
 		var camposts = new Array(false, false, false, false, false);
 		var retorno = true;

 		function verifmat() {
 			var ismat = RegExp(/^\d*[0-9]$/);
 		 	mat = document.getElementById('matricula').value;
 		 	if (!(ismat.test(mat)) || (mat+"").length > 10) {
			 	document.getElementById('matricula').style.backgroundColor='#FFC2E6';
					camposts[0] = false;
			    }else{
			    	document.getElementById('matricula').style.backgroundColor='#C0D9FC';
					camposts[0] = true;
			    }	
 		} 
 		
 		function verifnome() {
 			var tstnome= RegExp(/^[a-zA-Z\s]+$/);
 		 	nome = document.getElementById('nome').value;
 		 	if (!(tstnome.test(nome)) || nome.length < 5 ) {
			 	document.getElementById('nome').style.backgroundColor='#FFC2E6';
					camposts[1] = false;
			    }else{
			    	document.getElementById('nome').style.backgroundColor='#C0D9FC';
					camposts[1] = true;
			    }	
 		}

 		function verifuser() {
 		 	user = document.getElementById('usuario').value;
 		 	if (user == "" || user.length < 5 ) {
			 	document.getElementById('usuario').style.backgroundColor='#FFC2E6';
					camposts[2] = false;
			    }else{
			    	document.getElementById('usuario').style.backgroundColor='#C0D9FC';
					camposts[2] = true;
			    }	
 		}

 		function verifsenha(){
 		 	senha = document.getElementById('senha').value;

 		 	if (senha == "" || senha.length < 5 || senha.length > 10) {
			 	document.getElementById('senha').style.backgroundColor='#FFC2E6';
					camposts[3] = false;
			}else{
			    document.getElementById('senha').style.backgroundColor='#C0D9FC';
				camposts[3] = true;
			}	
 		}
		
		function verifconsenha(){
 		 	consenha = document.getElementById('consenha').value;
 		 	senha = document.getElementById('senha').value;

 		 	if (consenha == "" || consenha.length < 5 || consenha.length > 10 ) {
			 	document.getElementById('consenha').style.backgroundColor='#FFC2E6';
					camposts[4] = false;
			}else if (consenha === senha) { 
				document.getElementById('consenha').style.backgroundColor='#C0D9FC';
				camposts[4] = true;
			}else{
			    document.getElementById('consenha').style.backgroundColor='#FFC2E6';
					camposts[4] = false;
			}	
 		}
		
		function valida () {
			 verifmat();
			 verifnome();
             verifuser();
             verifsenha();
             verifconsenha();

			for (var i = 0; i < camposts.length; i++) {
				if (!(camposts[i])) {
					retorno = false;
				}
			}
			
			
			if (retorno) {
				return true;
			}else{
				retorno = true;
				return false;
			}
		}

			 
 	</script>
 </head>
 <body>
		<form action="index.php?page=funcionario/cnovofuncionario.php" method="post" onsubmit='return valida()'>
 		<fieldset id="formulario" class="field"> 
 		<legend> Cadastro de Novo Funcionário</legend>
			<label for="matricula">Matricula: </label> <input type="text" name="matricula" id="matricula" onblur="verifmat()"><br/>
			<label for="nome">Nome: </label><input type="text" name="nome" id="nome" onblur="verifnome()"><br/>
			<label for="usuario">Usuario: </label><input type="text" name="usuario" id="usuario" onblur="verifuser()"><br/>
			<label for="senha">Senha: </label><input type="password" name="senha" id="senha" onblur="verifsenha()"><br/>
			<label for="consenha">Confirme a Senha: </label><input type="password" name="consenha" id="consenha" onblur="verifconsenha()"><br/>
			<label for="funcao">Função: </label>
				<SELECT name="funcao" id="funcao">
				<?php while ($arrayresultado2 = mysql_fetch_assoc($resultado)): ?>
				<option value="<?php echo $arrayresultado2['idfuncao']; ?>">
					<?php echo $arrayresultado2['descricaofunc']; ?>
				</option>
				<?php endwhile ?>
				</SELECT><br/>
			<label for="permissao">Permissao: </label>
				<SELECT name="permissao" id="permissao">
					<?php while ($arrayperm = mysql_fetch_assoc($resulperm)): ?>
					<option  value="<?php echo $arrayperm['idpermissoes']; ?>">
						<?php echo $arrayperm['descricao']; ?>
					</option>
						 				
					<?php endwhile ?>
					 			</SELECT><br/>
					<input type="reset" value="Limpar">&nbsp &nbsp
					<input id="espaco" type="submit" value="Salvar">
				 		</form>
		</fieldset>
 </body>
 </html>

 